package com.awise.app.manual.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.awise.R;
import com.awise.base.BaseActivity;
import com.awise.http.RoverGlobal;
import com.awise.util.Constant;


/**
 * Created by lgz on 2018/1/23.
 */

public class ManualActivity extends BaseActivity implements View.OnClickListener, SeekBar
        .OnSeekBarChangeListener {
    private RelativeLayout manual_seekbar_a,manual_seekbar_b,manual_seekbar_c,manual_seekbar_d,manual_seekbar_e;
    private TextView manual_tv_id_a,manual_tv_id_b,manual_tv_id_c,manual_tv_id_d,manual_tv_id_e;
    private TextView manual_tv_countr_a,manual_tv_countr_b,manual_tv_countr_c,manual_tv_countr_d,manual_tv_countr_e;
    private SeekBar manual_sb_seekbar_a,manual_sb_seekbar_b,manual_sb_seekbar_c,manual_sb_seekbar_d,manual_sb_seekbar_e;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manual_layout);
        initview();
        showLoadingView();
        // 注册广播
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(Constant.MANUAL_ACTION));
    }


    @Override
    protected void onStart() {
        super.onStart();
        readData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 解除注册广播
        LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
    }

    private void readData() {
        // 读取手动值
        // c0 0a 11 01 1a 16 00 00 校验和
        byte[] bytes = {(byte) 0xc0, 0x0a, 0x11, 0x01, 0x1a, 0x16, 0x00, 0x00, 0x00};
        bytes[bytes.length-1] = RoverGlobal.getInstance().getAddValue(bytes, bytes.length-1);
        RoverGlobal.getInstance().connect.write(bytes);
    }


    private void initview() {
        TextView  s_back = (TextView) this.findViewById(R.id.tv_back);
        TextView  textView_content = (TextView) this.findViewById(R.id.textView_content);
        textView_content.setText(R.string.manual_title);
        manual_seekbar_a = (RelativeLayout) this.findViewById(R.id.manual_seekbar_a);
        manual_seekbar_b = (RelativeLayout) this.findViewById(R.id.manual_seekbar_b);
        manual_seekbar_c = (RelativeLayout) this.findViewById(R.id.manual_seekbar_c);
        manual_seekbar_d = (RelativeLayout) this.findViewById(R.id.manual_seekbar_d);
        manual_seekbar_e = (RelativeLayout) this.findViewById(R.id.manual_seekbar_e);
        s_back.setVisibility(View.VISIBLE);
        s_back.setOnClickListener(this);
        setnumber(getSP(this,"ControlNumber", 5));
        manual_tv_id_a = (TextView) manual_seekbar_a.findViewById(R.id.manual_tv_id);
        manual_tv_countr_a = (TextView) manual_seekbar_a.findViewById(R.id.manual_tv_count);
        manual_sb_seekbar_a = (SeekBar) manual_seekbar_a.findViewById(R.id.manual_sb_seekbar);
        manual_tv_id_b = (TextView) manual_seekbar_b.findViewById(R.id.manual_tv_id);
        manual_tv_countr_b = (TextView) manual_seekbar_b.findViewById(R.id.manual_tv_count);
        manual_sb_seekbar_b = (SeekBar) manual_seekbar_b.findViewById(R.id.manual_sb_seekbar);
        manual_tv_id_c = (TextView) manual_seekbar_c.findViewById(R.id.manual_tv_id);
        manual_tv_countr_c = (TextView) manual_seekbar_c.findViewById(R.id.manual_tv_count);
        manual_sb_seekbar_c = (SeekBar) manual_seekbar_c.findViewById(R.id.manual_sb_seekbar);
        manual_tv_id_d = (TextView) manual_seekbar_d.findViewById(R.id.manual_tv_id);
        manual_tv_countr_d = (TextView) manual_seekbar_d.findViewById(R.id.manual_tv_count);
        manual_sb_seekbar_d = (SeekBar) manual_seekbar_d.findViewById(R.id.manual_sb_seekbar);
        manual_tv_id_e = (TextView) manual_seekbar_e.findViewById(R.id.manual_tv_id);
        manual_tv_countr_e = (TextView) manual_seekbar_e.findViewById(R.id.manual_tv_count);
        manual_sb_seekbar_e = (SeekBar) manual_seekbar_e.findViewById(R.id.manual_sb_seekbar);
        manual_tv_countr_a.setText(manual_sb_seekbar_a.getProgress()+"%");
        manual_tv_countr_b.setText(manual_sb_seekbar_b.getProgress()+"%");
        manual_tv_countr_c.setText(manual_sb_seekbar_c.getProgress()+"%");
        manual_tv_countr_d.setText(manual_sb_seekbar_d.getProgress()+"%");
        manual_tv_countr_e.setText(manual_sb_seekbar_e.getProgress()+"%");
        manual_sb_seekbar_a.setOnSeekBarChangeListener(this);
        manual_sb_seekbar_b.setOnSeekBarChangeListener(this);
        manual_sb_seekbar_c.setOnSeekBarChangeListener(this);
        manual_sb_seekbar_d.setOnSeekBarChangeListener(this);
        manual_sb_seekbar_e.setOnSeekBarChangeListener(this);
        manual_tv_id_a.setText("A");
        manual_tv_id_b.setText("B");
        manual_tv_id_c.setText("C");
        manual_tv_id_d.setText("D");
        manual_tv_id_e.setText("E");
    }

    private void setnumber(int number) {
        switch (number){
            case 1:
                manual_seekbar_a.setVisibility(View.VISIBLE);
                manual_seekbar_b.setVisibility(View.GONE);
                manual_seekbar_c.setVisibility(View.GONE);
                manual_seekbar_d.setVisibility(View.GONE);
                manual_seekbar_e.setVisibility(View.GONE);
                break;
            case 2:
                manual_seekbar_a.setVisibility(View.VISIBLE);
                manual_seekbar_b.setVisibility(View.VISIBLE);
                manual_seekbar_c.setVisibility(View.GONE);
                manual_seekbar_d.setVisibility(View.GONE);
                manual_seekbar_e.setVisibility(View.GONE);
                break;
            case 3:
                manual_seekbar_a.setVisibility(View.VISIBLE);
                manual_seekbar_b.setVisibility(View.VISIBLE);
                manual_seekbar_c.setVisibility(View.VISIBLE);
                manual_seekbar_d.setVisibility(View.GONE);
                manual_seekbar_e.setVisibility(View.GONE);
                break;
            case 4:
                manual_seekbar_a.setVisibility(View.VISIBLE);
                manual_seekbar_b.setVisibility(View.VISIBLE);
                manual_seekbar_c.setVisibility(View.VISIBLE);
                manual_seekbar_d.setVisibility(View.VISIBLE);
                manual_seekbar_e.setVisibility(View.GONE);
                break;
            case 5:
                manual_seekbar_a.setVisibility(View.VISIBLE);
                manual_seekbar_b.setVisibility(View.VISIBLE);
                manual_seekbar_c.setVisibility(View.VISIBLE);
                manual_seekbar_d.setVisibility(View.VISIBLE);
                manual_seekbar_e.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_back:
                finish();
                break;
        }
    }
    private int progress1,progress2,progress3,progress4,progress5;
    private  long startTime,endTime;

    private boolean  seekbarA=false;
    private boolean  seekbarB=false;
    private boolean  seekbarC=false;
    private boolean  seekbarD=false;
    private boolean  seekbarE=false;
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        startTime = endTime;
        if (seekBar.equals(manual_sb_seekbar_a)){
            progress1=progress;
            manual_tv_countr_a.setText(progress+"%");
            if (seekbarA){
                endTime=System.currentTimeMillis();
                if (endTime-startTime>20)
                onOff((byte)progress1,(byte)progress2,(byte)progress3,(byte)progress4,(byte)progress5);
            }
            seekbarA=true;
        }else if (seekBar.equals(manual_sb_seekbar_b)){
            progress2=progress;
            manual_tv_countr_b.setText(progress+"%");
            if (seekbarB){
                endTime=System.currentTimeMillis();
                if (endTime-startTime>20)
                onOff((byte)progress1,(byte)progress2,(byte)progress3,(byte)progress4,(byte)progress5);
            }
            seekbarB=true;
        }else if (seekBar.equals(manual_sb_seekbar_c)){
            progress3=progress;
            manual_tv_countr_c.setText(progress+"%");
            if (seekbarC){
                endTime=System.currentTimeMillis();
                if (endTime-startTime>20)
                onOff((byte)progress1,(byte)progress2,(byte)progress3,(byte)progress4,(byte)progress5);
            }
            seekbarC=true;
        }else if (seekBar.equals(manual_sb_seekbar_d)) {
            progress4=progress;
            manual_tv_countr_d.setText(progress+"%");
            if (seekbarD){
                endTime=System.currentTimeMillis();
                if (endTime-startTime>20)
                onOff((byte)progress1,(byte)progress2,(byte)progress3,(byte)progress4,(byte)progress5);
            }
            seekbarD=true;
        }else if(seekBar.equals(manual_sb_seekbar_e)){
            progress5=progress;
            manual_tv_countr_e.setText(progress+"%");
            if (seekbarE){
                endTime=System.currentTimeMillis();
                if (endTime-startTime>20)
                onOff((byte)progress1,(byte)progress2,(byte)progress3,(byte)progress4,(byte)progress5);
            }
            seekbarE=true;
        }
    }


    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        onOff((byte)progress1,(byte)progress2,(byte)progress3,(byte)progress4,(byte)progress5);
    }

    public void onOff(byte value1,byte value2,byte value3,byte value4,byte value5) {
        System.out.println("value1:"+value1+"  "+value2+"  "+value3+"  "+value4+" "+value5);
        byte[] bytes = {(byte) 0xc0, 0x0a, 0x11, 0x01, 0x16, 0x00, 0x00, 0x05, value1,value2,value3,value4,value5, 0x00};
        bytes[bytes.length-1] = RoverGlobal.getInstance().getAddValue(bytes, bytes.length);
        RoverGlobal.getInstance().connect.write(bytes);
       /* 读取手动值
        c0 0a 11 01 1a 16 00 00 校验和
        手动调光 （100%）：
        c0 0a 11 01 16 00 00 05 64 64 64 64 64 EB
        手动调光 （ 0% ）
        c0 0a 11 01 16 00 00 05 00 00 00 00 00 F7*/
    }
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            closedLoadingView();
            if (intent.getAction() == Constant.MANUAL_ACTION) {
                byte[] buffer = intent.getByteArrayExtra("socket_data");
                if (buffer != null) {
                    byte buffer4 = buffer[4];
                    byte buffer5 = buffer[5];
                    switch (buffer4) {
                        case 0x1a:
                            if (buffer5 == 0x16) { // 读取手动
                                int dataLenght = buffer[7];
                                byte  value1 = buffer[8];
                                byte  value2 = buffer[9];
                                byte  value3 = buffer[10];
                                byte  value4 = buffer[11];
                                byte  value5 = buffer[12];
                                System.out.println("progress:"+value1+"  "+value2+"  "
                                        +value3+"  "+value4+"  "+value5);
                                manual_sb_seekbar_a.setProgress(value1);
                                manual_sb_seekbar_b.setProgress(value2);
                                manual_sb_seekbar_c.setProgress(value3);
                                manual_sb_seekbar_d.setProgress(value4);
                                manual_sb_seekbar_e.setProgress(value5);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    };


}
